﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MINERP.BLL.Statistics;
using MINERP.MODEL.Statistics;
using DevExpress.XtraGrid.Views.Grid;

namespace MINERP.Statistics
{
    public partial class FrmAlarmMessage : Form
    {
        private static FrmAlarmMessage instance = null;
        private AlarmMessageBLL _bll = new AlarmMessageBLL();
        private AlarmMessageInfo _alarmMessageInfo = null;
        private bool _onlyShowNoRead = false;
        private int _userid;


        private FrmAlarmMessage()
        {
            InitializeComponent();
        }

        public static FrmAlarmMessage GetInstance
        {
            get 
            {
                if (instance == null)
                {
                    instance = new FrmAlarmMessage();
                }
                return instance;
            }
        }

        public int UserId
        {
            set { _userid = value; }
        }

        public bool OnlyShowNoRead
        {
            set { _onlyShowNoRead = value; }
        }
        private void FrmAlarmMessage_Load(object sender, EventArgs e)
        {
            if (!_onlyShowNoRead)
            {
                this.gridViewAlarm.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(this.gridViewAlarm_RowStyle);
            }
            LinkGridControl();
        }

        private void tsUpdate_Click(object sender, EventArgs e)
        {

        }

        private void gridViewAlarm_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _alarmMessageInfo = (AlarmMessageInfo)gridViewAlarm.GetFocusedRow();
        }

        private void LinkGridControl()
        {
            gridControlAlarm.DataSource = _bll.GetAlarmMessageInfoList(_userid);
        }

        private void gridViewAlarm_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView gv = (GridView)sender;
            string status = gv.GetRowCellDisplayText(e.RowHandle, gv.Columns.ColumnByFieldName("Status"));
            if (status == "0")
            {
                e.Appearance.BackColor = Color.Green;
            }
        }

        private void FrmAlarmMessage_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            Cursor = Cursors.WaitCursor;
            try
            {
                int [] handels = gridViewAlarm.GetSelectedRows();
                for (int i = 0; i < handels.Length; i++)
                {
                    AlarmMessageInfo al=(AlarmMessageInfo) gridViewAlarm.GetRow(handels[i]);
                    _bll.Update(al.Id);
                }
                LinkGridControl();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Cursor = Cursors.Default;
            btnUpdate.Enabled = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (DataCheck())
            {
                DateTime start = dtStart.DateTime;
                DateTime end = dtEnd.DateTime;
                int status;
                string statusName = cmbStatus.Text;
                if (statusName == "已读")
                {
                    status = 1;
                }
                else
                {
                    status = 0;
                }
                try
                {
                    gridControlAlarm.DataSource = _bll.Select(_userid, status, start, end);
                    gridControlAlarm.RefreshDataSource();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

            }
        }

        private bool DataCheck()
        {
            if (dtStart.Text == string.Empty)
            {
                MessageBox.Show("请选择开始时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtEnd.Text == string.Empty)
            {
                MessageBox.Show("请选择结束时间", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (cmbStatus.Text == string.Empty)
            {
                MessageBox.Show("请选择消息状态", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
    }
}
